package fy.uap.tree;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;

public class RootBean {

	private List<NodeBean> items;

	public List<NodeBean> getItems() {
		return items;
	}

	public void setItems(List<NodeBean> items) {
		this.items = items;
	}

	public void addItem(NodeBean item) {
		if (items == null) {
			items = new ArrayList<NodeBean>();
		} else
			for (int i = 0, length = items.size(); i < length; i++) {
				if (items.get(i).getId().equals(item.getId())) {
					return;
				}
			}
		items.add(item);
	}

	public void addItems(Map<String, List<NodeBean>> nodeMap) {
		if (items != null) {
			for (int i = 0, size = items.size(); i < size; i++) {
				NodeBean node = items.get(i);

				node.setItems(nodeMap.get(node.getId()));
				node.addItems(nodeMap);
			}
		}
	}

	public Element toXMLElement() {

		Document doc = org.dom4j.DocumentHelper.createDocument();

		Element node = doc.addElement("tree");
		node.addAttribute("id", "0");

		if (this.getItems() != null) {
			for (int i = 0, size = this.getItems().size(); i < size; i++) {
				this.getItems().get(i).toXMLElement(node);
			}
		}
		return node;
	}

	public Element toXMLElement(boolean disabled) {

		Document doc = org.dom4j.DocumentHelper.createDocument();

		Element node = doc.addElement("tree");
		node.addAttribute("id", "0");

		if (this.getItems() != null) {
			for (int i = 0, size = this.getItems().size(); i < size; i++) {
				this.getItems().get(i).toXMLElement(node, disabled);
			}
		}
		return node;
	}
}
